<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
       
<script>
<!--
jQuery().ready(function(){	
	createToken();
});

function createToken(){
	$.post('Token_createToken', function(result){
		result = eval('('+result+')');
		if (result.success){
			$('#htoken').val(result.msg);
		}
	});
}

// 字节长度
function strlength(str){
	var l=str.length;
	var n=l
	for (var i=0;i<l;i++){
		if (str.charCodeAt(i)<0||str.charCodeAt(i)>255) n++
	}
	return n
}

function changebyte(id,length, showLessLen){
	var value=document.getElementById(id).value;
	var l=document.getElementById(id).value.length;
	if (l==0){
		document.getElementById(showLessLen).innerHTML = "";
	} else if (l<=length){
		document.getElementById(showLessLen).innerHTML = "还可以输入"+(length-l)+"字节";
	} else{
		document.getElementById(id).value = document.getElementById(id).value.substring(0,length);		
		document.getElementById(showLessLen).innerHTML = '输入字节数超出范围';
	}
	return true
}

function msg_send(){

}

function msg_fm_submit() {
	$('#fat-btn').button('loading');
	var msg = $('#txtmsg').val();
	var url = 'mircblog/Message_postMsg?';
				
	if (msg.length>250) return false;			
	if (msg.length==0){
		Pub_alert(
			'alert',
			'警告',
			'发布微博不能为空!',
		$('#msg_alert'));
		$('#fat-btn').button('reset');	
		return false;
	}
	
	url = url + 'msg.MT_M_ID=' + $('#msg_m_id').val();
	url = url + '&token=' + $('#htoken').val();
	url = url + '&msg.M_CONTENT=' + $('#txtmsg').val();	
	url = url + '&msg.M_ANNEX=' + $('#msg_m_annex').val();
	$.ajax({  
	    type:"POST",  
	    dataType:"text",  
	    url:url,  
	    timeout:80000,     //ajax请求超时时间80秒  
	    async: false,
	    data:{time:"80"}, //40秒后无论结果服务器都返回数据  
	    success:function(data,textStatus){	    	    
			createToken();						
			$('#txtmsg').val('');
			document.getElementById("lessLen").innerHTML = "";
			
			data = eval('('+data+')');
			
			if (data.success){				
				var s_html = $('#msgcontent').html();
				s_html = showMessage(data.children[0],-1) + s_html;	
				$('#msgcontent').html(s_html)
				MessageDWR.send(data.msg, reply);										
			} else {
				Pub_alert(
					'alert',
					'警告',
					data.msg,
				$('#msg_alert'));	
			}
			$('#fat-btn').button('reset');
	    },  
		 //Ajax请求超时，继续查询  
		 error:function(XMLHttpRequest,textStatus,errorThrown){  
	        if(textStatus=="timeout"){
				Pub_alert(
					'alert alert-error',
					'错误',
					'网络超时!',
				$('#msg_alert'));
				$('#fat-btn').button('reset');	
	        }  
		 }  
	      
	});					
}

var reply = function(data){
	if (data != null && typeof data == 'object') {
		alert('hello');
		alert(dwr.util.toDescriptiveString(data, 2));
	}
	//else dwr.util.setValue('d0', dwr.util.toDescriptiveString(data, 1));
}
//-->
</script>

<form id="msg_fm" class="well" method="post">
	<div id="msg_alert">
	</div>
	<span id="lessLen"></span>
	<input type="hidden" id="msg_m_id" name="msg.MT_M_ID" value="-1"/>
	<input type="hidden" id="msg_m_annex" value="0"/>
	<input type="hidden" id="htoken" name="token" />
	<div class="control-group">	
		<textarea id="txtmsg" name="msg.M_CONTENT" class="twitter-anywhere-tweet-box-editor" 
		style="width: 80%; overflow-x: hidden; overflow-y: hidden; height: 55px; padding-bottom: 5px; " 
		dir="ltr" placeholder="说点自己的idea..." 
		onkeydown="return changebyte('txtmsg',250,'lessLen')" 
		onkeyup="return changebyte('txtmsg',250, 'lessLen')"></textarea>		
	</div>
	<div>		
	
	<a id="fat-btn" data-loading-text="正在发布..." class="btn btn-primary" onclick="msg_fm_submit()">发布</a>
	<!--  MessageDWR.getTest(reply)-->
	</div>	
</form>



 